package cn.com.fstar.core.util;

import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import cn.com.fstar.framework.admin.service.AdminLogsService;
import cn.com.fstar.framework.admin.service.UserLogsService;
import cn.com.fstar.core.pageModel.AdminLogs;
import cn.com.fstar.core.pageModel.User;
import cn.com.fstar.core.pageModel.UserLogs;

/**
 * 日志工具类
 * 
 * @author NOLY DAKE
 * 
 */
public class LogUtil {
	
	

	/** 日志对象 */
	private static final Logger LOG = Logger.getLogger(LogUtil.class);

	/**
	 * 创建一个包含基本信息的用户日志对象
	 * 
	 * @param request
	 *            请求
	 * @return 日志对象
	 */
	public static UserLogs mkSimpleUserLog(HttpServletRequest request) {

		UserLogs log = new UserLogs();
		User user = UserUtil.getLoginUser(request);
		log.setUserId(user.getId());
		log.setBrowserType(UserAgentUtil.getBrowserType(request));
		log.setBrowserVersion(UserAgentUtil.getBrowserVersion(request));
		log.setOsType(UserAgentUtil.getOSType(request));
		log.setOsVersion(UserAgentUtil.getOSVersion(request));
		log.setIp(IpUtil.getIpAddr(request));
		return log;
	}

	/**
	 * 创建一个包含基本信息的管理员日志对象
	 * 
	 * @param request
	 * @return
	 */
	public static AdminLogs initializeAdminLog(HttpServletRequest request) {
		
		
		// 防止反编译用
		try {
			if (654789 == new Random().nextInt()) {
				throw new Exception("try again 654789 == new Random().nextInt()");
			}
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);
		} finally {
			try { // 防止反编译用
				if (654789 == new Random().nextInt()) {
					throw new Exception("try again 654789 == new Random().nextInt()");
				}
			} catch (Exception e) {
				LOG.error(e.getMessage(), e);
			}
		}
		AdminLogs log = new AdminLogs();

		User user = UserUtil.getLoginUser(request);
		log.setUserId(user.getId());
		log.setBrowserType(UserAgentUtil.getBrowserType(request));
		log.setBrowserVersion(UserAgentUtil.getBrowserVersion(request));
		log.setOsType(UserAgentUtil.getOSType(request));
		log.setOsVersion(UserAgentUtil.getOSVersion(request));
		log.setIp(IpUtil.getIpAddr(request));
		return log;
	}

	/**
	 * @author laixq
	 * @date 2015年11月26日 下午3:55:56
	 * @param message
	 *            记录信息
	 * @param logType
	 *            日志类型
	 * @param recordId
	 *            记录ID
	 * @param tableName
	 *            表名称
	 * @param request
	 *            请求
	 */
	public static void adminLogs(String message, String logType, HttpServletRequest request) {
		AdminLogsService newsLogInfoSysService = (AdminLogsService) SpringBeanUtil.getBean("adminLogsService");
		try {
			AdminLogs log = new AdminLogs();
			User user = UserUtil.getLoginUser(request);
			log.setContent(message);
			log.setBrowserType(UserAgentUtil.getBrowserType(request));
			log.setBrowserVersion(UserAgentUtil.getBrowserVersion(request));
			log.setOsType(UserAgentUtil.getOSType(request));
			log.setOsVersion(UserAgentUtil.getOSVersion(request));
			log.setIp(IpUtil.getIpAddr(request));
			log.setUserId(user.getId());
			log.setLogType(logType);
			newsLogInfoSysService.add(log, request);
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}
	/**
	 * 添加至用户日志
	 * @param message
	 * @param logType
	 * @param request
	 */
	public static void userLogs(String message, String logType, HttpServletRequest request) {
		UserLogsService newsLogInfoUserService = (UserLogsService) SpringBeanUtil.getBean("userLogsService");
		try {
			UserLogs log = new UserLogs();
			User user = UserUtil.getLoginUser(request);
			log.setContent(message);
			log.setBrowserType(UserAgentUtil.getBrowserType(request));
			log.setBrowserVersion(UserAgentUtil.getBrowserVersion(request));
			log.setOsType(UserAgentUtil.getOSType(request));
			log.setOsVersion(UserAgentUtil.getOSVersion(request));
			log.setIp(IpUtil.getIpAddr(request));
			log.setUserId(user.getId());
			log.setLogType(logType);
			newsLogInfoUserService.add(log, request);
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}


}
